package com.fhzn.boss.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhzn.boss.entity.App;
import com.fhzn.boss.entity.WebResponse;
import com.fhzn.boss.service.AppService;
import com.fhzn.boss.tools.PageInfoUtils;
import com.fhzn.boss.web.converter.AppConverter;
import com.fhzn.boss.web.request.AppOpRequest;
import com.fhzn.boss.web.request.AppQueryRequest;
import com.fhzn.boss.web.vo.AppVO;
import com.fhzn.commons.toolkit.entity.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;

/**
 * @author 老顾
 * @apiNote 应用管理
 * @since 2024/1/3
 */
@RestController
@RequestMapping("app")
@RequiredArgsConstructor
public class ApplicationController {

    private final AppService appService;

    @GetMapping("/query")
    public WebResponse<PageInfo<AppVO>> query(@ParameterObject AppQueryRequest request) {
        Page<App> appPage = appService.appWithPage(request);
        return WebResponse.success(PageInfoUtils.convert2page(appPage, AppConverter::fromApp));
    }

    @PostMapping("/add")
    public WebResponse<AppVO> add(@RequestBody AppOpRequest request) {
        return WebResponse.success(appService.add(request));
    }

    @GetMapping("/queryOne")
    public WebResponse<AppVO> queryOne(@ParameterObject AppQueryRequest request) {
        return WebResponse.success(appService.queryOne(request));
    }

    @PostMapping("/update")
    public WebResponse<String> update(@RequestBody AppOpRequest request) {
        return WebResponse.success(appService.update(request));
    }

    @GetMapping("/export")
    public ResponseEntity<byte[]> export(@NotEmpty String code, HttpServletResponse response) throws IOException {
        return appService.export(code);
    }
}
